package org.weatherapplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

public class WeatherApplicationActivity extends ListActivity {
    private final static String FILENAME = "cities.txt";
	private CitiesListAdapter clAdapter;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        clAdapter = new CitiesListAdapter(this);
        setListAdapter(clAdapter);
        
        //verificarea existentei fisierului in care este memorata lista de orase
        //crearea lui in cazul in care nu exista
        File file = getApplicationContext().getFileStreamPath(FILENAME);
        if (!file.exists()) {
        	try {
        		OutputStreamWriter out = new OutputStreamWriter(openFileOutput(FILENAME, MODE_PRIVATE));
        		out.close();
        	} catch (Throwable t) {
        		Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_SHORT).show();
        	}
        }
        
        getListView().setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> listAdapter, View v, int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("city", (String) clAdapter.getItem(position)); 
				intent.setClass(WeatherApplicationActivity.this, CityWeatherActivity.class);
				startActivity(intent);
			}
		});
        
        getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> listAdapter, View v, int position, long id) {
				return clAdapter.removeCity(position);
			}
		});
    }
    
    @Override
    public void onActivityResult(int requestCode, int responseCode, Intent data) {
    	if (requestCode == 100) {
    		//codul 1 corespunde butonului Add
    		if (responseCode == 1) {
    			//adaugarea numelui orasului introdus in activitatea AddCityActivity si transmis prin Intent
    			clAdapter.addCity(data.getStringExtra("cityToBeAdded"));
    		}
    	}
    }
    
    /** Pornirea activitatii AddCityActivity si asteptarea unui raspuns al acesteia. */
    public void startAddCityActivity(View v) {
    	Intent addCity = new Intent();
    	addCity.setClass(this, AddCityActivity.class);
    	startActivityForResult(addCity, 100);
    }
    
    public void onResume() {
    	super.onResume();
    	
    	try {
    		InputStream in = openFileInput(FILENAME);
    		
    		if (in != null) {
    			InputStreamReader isr = new InputStreamReader(in);
    			BufferedReader buffReader = new BufferedReader(isr);
    			String str;
    			
    			while ((str = buffReader.readLine()) != null) {
    				clAdapter.addCity(str);
    			}
    			
    			in.close();
    		}
    	} catch (IOException e) {
    		Toast.makeText(this, "Exception: " + e.toString(), Toast.LENGTH_SHORT).show();
    	}
    }
    
    public void onPause() {
    	super.onPause();
    	
    	try {
    		OutputStreamWriter out = new OutputStreamWriter(openFileOutput(FILENAME, MODE_PRIVATE));
    		out.write(clAdapter.toString());
    		out.close();
    	} catch (Throwable t) {
    		Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_SHORT).show();
    	}
    }
}